package cn.edu.scau.cmi.huxiantao.composite;

import java.net.URL;
import java.util.HashSet;
import java.util.ResourceBundle;
import java.util.Set;

import cn.edu.scau.cmi.huxiantao.composite.consistent.ConsistentComponent;
import cn.edu.scau.cmi.huxiantao.composite.consistent.ConsistentOrganization;
import cn.edu.scau.cmi.huxiantao.composite.consistent.ConsistentPerson;
import cn.edu.scau.cmi.huxiantao.composite.safe.SafeComponent;
import cn.edu.scau.cmi.huxiantao.composite.safe.SafeOrganization;
import cn.edu.scau.cmi.huxiantao.composite.safe.SafePerson;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class GUIController implements Initializable{
	@FXML
	private ChoiceBox<String> choiceBox1;
	@FXML
	private ChoiceBox<String> choiceBox2;
	@FXML
	private TextField name;
	@FXML
	private TextField childName;
	@FXML
	private ListView<String> listView;
	@FXML
	private ImageView imageView;
	
	private ObservableList<String> list;
	
	private Set<SafeComponent> safeComponentSet=new HashSet<SafeComponent>();
	private Set<ConsistentComponent> consistentComponentSet=new HashSet<ConsistentComponent>();
	
	@FXML
	private void create() {
		String name=this.name.getText();
		if(name.equals("")) {
			return;
		}
		if(choiceBox1.getValue().equals("安全组合模式")){
			if(choiceBox2.getValue().equals("组织")) {
				safeComponentSet.add(new SafeOrganization(name));
			}else {
				safeComponentSet.add(new SafePerson(name));
			}
		}else {
			if(choiceBox2.getValue().equals("组织")) {
				consistentComponentSet.add(new ConsistentOrganization(name));
			}else {
				consistentComponentSet.add(new ConsistentPerson(name));
			}
		}
		list.add("创建成功");
	}
	@FXML
	private void addChild() {
		String parent=name.getText();
		String child=childName.getText();
		String result="添加失败";
		if(parent.equals(child)) return;
		if(choiceBox1.getValue().equals("安全组合模式")){
			for(SafeComponent c:safeComponentSet) {
				if(c.getName().equals(parent)&&c.getClass()==SafeOrganization.class) {
					SafeComponent childComponent=null;
					for(SafeComponent c1:safeComponentSet) {
						if(c1.getName().equals(child)) {
							childComponent=c1;
							break;
						}
					}
					if(childComponent!=null) {
						((SafeOrganization)c).addChild(childComponent);
						result="添加成功";
					}
					break;
				}
			}
		}else {
			for(ConsistentComponent c:consistentComponentSet) {
				if(c.getName().equals(parent)&&c.getClass()==ConsistentOrganization.class) {
					ConsistentComponent childComponent=null;
					for(ConsistentComponent c1:consistentComponentSet) {
						if(c1.getName().equals(child)) {
							childComponent=c1;
							break;
						}
					}
					if(childComponent!=null) {
						((ConsistentOrganization)c).addChild(childComponent);
						result="添加成功";
					}
					break;
				}
			}
		}
		list.add(result);
	}
	@FXML
	private void deleteChild() {
		String parent=name.getText();
		String child=childName.getText();
		String result="删除失败";
		if(choiceBox1.getValue().equals("安全组合模式")){
			for(SafeComponent c:safeComponentSet) {
				if(c.getName().equals(parent)&&c.getClass()==SafeOrganization.class) {
					SafeComponent childComponent=null;
					for(SafeComponent c1:safeComponentSet) {
						if(c1.getName().equals(child)) {
							childComponent=c1;
							break;
						}
					}
					((SafeOrganization)c).deleteChild(childComponent);
					result="删除成功";
					break;
				}
			}
		}else {
			for(ConsistentComponent c:consistentComponentSet) {
				if(c.getName().equals(parent)&&c.getClass()==ConsistentOrganization.class) {
					ConsistentComponent childComponent=null;
					for(ConsistentComponent c1:consistentComponentSet) {
						if(c1.getName().equals(child)) {
							childComponent=c1;
							break;
						}
					}
					((ConsistentOrganization)c).deleteChild(childComponent);
					result="删除成功";
					break;
				}
			}
		}
		list.add(result);
	}
	@FXML
	private void doSomething() {
		String name=this.name.getText();
		if(name.equals("")) {
			return;
		}
		String result="";
		if(choiceBox1.getValue().equals("安全组合模式")){
			for(SafeComponent c:safeComponentSet) {
				if(name.equals(c.getName())) {
					result=c.doSomething();
					break;
				}
			}
		}else {
			for(ConsistentComponent c:consistentComponentSet) {
				if(name.equals(c.getName())) {
					result=c.doSomething();
					break;
				}
			}
		}
		list.add(result);
	}
	
	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		list=FXCollections.observableArrayList();
		listView.setItems(list);
		
		choiceBox1.setItems(FXCollections.observableArrayList("安全组合模式","一致性组合模式"));
		choiceBox1.setValue("安全组合模式");

		choiceBox2.setItems(FXCollections.observableArrayList("组织","个人"));
		choiceBox2.setValue("组织");
		
		imageView.setImage(new Image("/cn/edu/scau/cmi/huxiantao/pic/pic.jpg"));
	}
	
}
